package com.boss.ccs.gis;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import com.boss.core.Boss;
import com.boss.core.BossConstants;
import com.boss.core.BossDescriptor;
import com.boss.core.form.BossFormHelper;
import com.boss.core.manage.BossProperty;
import com.boss.core.manage.PropertySection;
import com.boss.core.util.BossGlobalUtil;
import com.boss.core.util.BossUtil;
import com.boss.Messages;

import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import hudson.Extension;
import hudson.util.ListBoxModel;

/**
 * This class supports the GIS-PAM view
 * @author Momin Noor Khan
 */

@Extension
public class BossGISPAM extends Boss {
    @Override
    public DescriptorImpl getDescriptor() {
        return (DescriptorImpl) super.getDescriptor();
    }

    @Override
    public String getDescription() {
        return "";
    }

    /**
     * Get the project name
     * @return
     */
    public String getProjectName() {
        return BossConstants.PROJECT_GIS;
    }

    /**
     * Check if the current user is authorized for GIS support tasks
     * @return
     */
    public static boolean isAuthorizedGISUser() {
        return Boss.isAuthorizedGISUser();
    }

    /**
     * Get the display label of this form
     * @return
     */
    public String getLabel() {
        return "GIS PAM Form";
    }

    /**
     * Get the parent tab of this form
     * @return
     */
    public String getTabView() {
        return BossConstants.TAB_GIS;
    }

    @Override
    public List<String> getTasks(String project) {
        return BossUtil.getPropertyValues2(project, "PAM-TASKS", '\n', true, false);
    }

    /**
     * Submit the form
     * @param req
     * @param rsp
     * @throws ServletException
     * @throws IOException
     */
    public void doSubmit(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
        JSONObject jsonForm = req.getSubmittedForm();
        String project = BossConstants.PROJECT_PAM;
        BossGISPAMForm form = new BossGISPAMForm(req, jsonForm, project);

        if (form.getJsonTaskSection() == null || StringUtils.isBlank(form.getTask())) {
            BossFormHelper.setFailureMsg(req, Messages.Boss_MissingTaskSection());
            req.getView(this, getPostSubmitView()).forward(req, rsp);
            return;
        }

        if (form.isValid()) {
            form.submit();
        }
        //Display the post-submit landing page
        req.getView(this, getPostSubmitView()).forward(req, rsp);
    }

    @Extension
    public static final class DescriptorImpl extends BossDescriptor {
        /**
         * Get the default target location on the GIS server
         * @param server
         * @return
         */
        public String getTargetLocation(String server) {
            String gisProfile = BossGlobalUtil.getGisProfilesPath() + server + BossConstants.SUFFIX_DOT_PROPERTIES;
            String targetLocation = BossUtil.getPropertyValue(BossConstants.GIS_DEFAULT_SERVER_HOME_KEY, gisProfile);
            return targetLocation;
        }

        public List<BossProperty> getGisCustomers() {
            PropertySection propertySection = BossGlobalUtil.getPropertySection(BossConstants.GIS_CUSTOMER_KEY);
            List<BossProperty> properties = propertySection.getProperties();
            return properties;
        }

        /**
         * Populates the module drop-down on the form, from the boss config
         * @return
         */
        public ListBoxModel doFillModuleNameItems() {
            return getListBoxModel(BossConstants.PROJECT_PAM, "MODULES");
        }

        /**
         * Populates the version drop-down on the form, from the boss config
         * @param moduleName
         * @return
         */
        public ListBoxModel doFillVersionNumberItems(@QueryParameter(BossConstants.KEY_MODULE_NAME) String moduleName) {
            ListBoxModel model = new ListBoxModel();
            String svnURL = BossGlobalUtil.getPropertyValue(BossConstants.PROJECT_PAM, BossConstants.SVN_STAGING_URL);
            String svnPath = BossGlobalUtil.getPropertyValue(BossConstants.PROJECT_PAM, BossConstants.SVN_STAGING_PATH) + "/" + moduleName;
            String svnUserName = BossGlobalUtil.getSvnUserName();
            String svnPassword = BossGlobalUtil.getSvnPassword();

            if (!StringUtils.isBlank(moduleName)) {
                model = getSVNDirListBoxModel(svnURL, svnPath, svnUserName, svnPassword, true);
            }
            return model;
        }
    }
}
